/**
  ******************************************************************************
  * @file    WF_GetIP.c
  * @author  exatron
  * @version V1.0.0
  * @date    segunda, 22 de dezembro de 2014
  * @brief   Devolve pela serial o IP da wlan0.
  ******************************************************************************
  * @attention
  *
  *
  ******************************************************************************
  */

/** @addtogroup WF
  * @{
  */

/* Includes ------------------------------------------------------------------*/
#include <common.h>

/* External functions --------------------------------------------------------*/

/** @defgroup WF_Functions
  * @{
  */

/**
  * @brief  Devolve pela serial o IP da wlan0.
  * @param  Nenhum
  * @retval Devolve pela serial o ip ou Result_t
  */
void WF_GetIP(void)
{
    int dSocket;                    /*!< Socket para manipulação de ioctl */
    struct ifreq xIRS;              /*!< Interface request structure */
    #ifdef DEBUG_ON_PC
    char *iface = "eth0";           /*!< Nome da interface */
    #else
    char *iface = "wlan0";          /*!< Nome da interface */
    #endif
    struct sockaddr_in *xIP;         /*!< Internet socket address */

    /* Zera a estrutura xIRS */
    memset(&xIRS, 0, sizeof(xIRS));
    /* Zera a estrutura xIP */
    memset(&xIP, 0, sizeof(xIP));

    /* Cria socket, família AF_INET, tipo sequencial */
    dSocket = socket(AF_INET, SOCK_STREAM, 0);
    if(dSocket != -1) // Socket Ok
    {
     	/* Informa a família ao xIRS */
		xIRS.ifr_addr.sa_family = AF_INET;
		/* Copia nome da interface para a estrutura xIRS */
		strcpy(xIRS.ifr_name, iface);
		/* Faz a requisição do comando SIOCGIFADDR ao I/O
		 * dSocket
		 */
		if(ioctl(dSocket, SIOCGIFADDR, &xIRS) != -1) // ioctl Ok
		{
			xIP = (struct sockaddr_in*)&xIRS.ifr_addr;
			aux_USART0_puts("%s=%d;%s\r\n", "WF_GIP", Result_Ok, inet_ntoa(xIP->sin_addr));
		}
		else // ioctl error
		{
			aux_USART0_puts("%s=%d\r\n", "WF_GIP", Result_IoctlError);
		}
		close(dSocket);
	}
	else // Socket error
	{
		aux_USART0_puts("%s=%d\r\n", "WF_GIP", Result_OnOpen);
	}

    aux_SetBusy(FALSE);
}

/**
  * @}
  */

/**
  * @}
  */

